package com.usian.comment.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Configuration
public class FeginConfig implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        // 1. 先获取请求中的token  固定代码 请求上下文
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        // 请求对象
        HttpServletRequest request = servletRequestAttributes.getRequest();
        // 2. 把获取到的token放到请求头中
        requestTemplate.header("token",request.getHeader("token"));
    }
}
